package com.sinba.common.user.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.util.StringUtils;

import com.sinba.common.dao.BaseDAOImplHibernate;
import com.sinba.common.dto.PageDTO;
import com.sinba.common.user.dto.UserDTO;
import com.sinba.common.user.entity.User;

public class UserDAO extends BaseDAOImplHibernate<User> implements IUserDAO {
	//分页
	public PageDTO findPager(UserDTO qdto,String sord, String sidx){
		
		final DetachedCriteria dc = DetachedCriteria.forClass( User.class );
        if ( qdto != null ){
        	if(StringUtils.hasText(qdto.getLoginName())){
        		dc.add( Restrictions.like("loginName",qdto.getLoginName(),MatchMode.ANYWHERE ) );
        	}
        }
        //排序
        if(sord!=null&&!sord.equals("")&&sidx!=null&&!sidx.equals("")){
            if(sord.equals("desc"))
            	dc.addOrder(Order.desc(sidx));
            else
            	dc.addOrder(Order.asc(sidx));
        }
        return super.findPageByCriteria( dc, qdto.getStart(), qdto.getLimit());
		
	};
	public User findByLoginAndPwd(UserDTO qdto){
		User user = new User();
		final DetachedCriteria dc = DetachedCriteria.forClass( User.class );
		if ( qdto != null ){
			dc.add(Restrictions.eq("loginName", qdto.getLoginName()));
			
			if(StringUtils.hasText(qdto.getSystemPassword())){
				dc.add(Restrictions.eq("systemPassword", qdto.getSystemPassword()));
			}else{
				dc.add(Restrictions.eq("password", qdto.getPassword()));
			}
		}
		List<User> list = super.getHibernateTemplate().findByCriteria(dc);
		if(list!=null && list.size()>0)
			user = list.get(0);
		else
			user = null;
		return user;
	}
	
}
